package com.share.xxl.job.executor.jobhandler;


import com.share.system.api.DeviceApi;
import com.share.system.api.domain.Region;
import com.share.system.api.domain.Station;
import com.share.system.api.service.RegionRedisService;
import com.share.system.api.service.StationRedisService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MysqlToRedisXxlJob {

    // 每次同步的分页大小
    private static final int PAGE_SIZE = 500;

    @Autowired
    private DeviceApi deviceApi;


    @Autowired
    private RegionRedisService regionRedisService;

    @Autowired
    private StationRedisService stationRedisService;


    /**
     * 采取线程池处理不同的分页 ，Redis Pipeline批量写入的操作，进行数据同步，根据时间戳进行判断是否进行更新
     * 1.获取同步表的数据条数
     * 2.创建线程池
     * 3.线程池分片进行同步
     * 4.批量写入Redis，并且进行判断是否进行更新
     * @param shardingParam 分片参数
     * @return 执行结果
     * @throws Exception 异常
     */
    @XxlJob("mysqlToRedisJobHandler")
    public ReturnT<String> mysqlToRedisJobHandler(String shardingParam) {
        try {
        log.info("mysqlToRedisJobHandler executed at: {}", System.currentTimeMillis());
        // 1. 获取分页总数
        int totalCount = deviceApi.getDeviceCount();
        int totalPages = (totalCount + PAGE_SIZE - 1) / PAGE_SIZE;
        // 2. 分页同步（避免全表加载OOM）
        for (int page = 1; page <= totalPages; page++) {
//            Page<Region> regionPage = new Page<>(page, PAGE_SIZE);
            List<Region> regions = deviceApi.getDeviceRegionListByPageAndWapper(page, PAGE_SIZE);
            int i = regionRedisService.batchSaveRegionsWithPipeline(regions);
            if (i == 0){
                log.info("mysqlToRedisJobHandler: {}", "没有数据更新");
            }
        }
            return ReturnT.ofSuccess();
        } catch (Exception e) {
            log.error("mysqlToRedisJobHandler执行异常: ", e);
            return ReturnT.ofFail(e.getMessage());
        }
    }


    @XxlJob("mysqlToRedisStationJobHandler")
    public ReturnT<String> mysqlToRedisStationJobHandler(String shardingParam) {
        try {
            log.info("mysqlToRedisJobHandler executed at: {}", System.currentTimeMillis());

            // 1. 获取分页总数
            int totalCount = deviceApi.getStationCount();
            int totalPages = (totalCount + PAGE_SIZE - 1) / PAGE_SIZE;
            // 2. 分页同步（避免全表加载OOM）
            for (int page = 1; page <= totalPages; page++) {
//            Page<Region> regionPage = new Page<>(page, PAGE_SIZE);
                List<Station> stationCountListByPageAndWapper = deviceApi.getStationCountListByPageAndWapper(page,
                        PAGE_SIZE);
                stationRedisService.batchSaveStationsWithPipeline(stationCountListByPageAndWapper);
                if (stationCountListByPageAndWapper.size() == 0){
                    log.info("mysqlToRedisJobHandler: {}", "没有数据更新");
                }
            }
            return ReturnT.ofSuccess();
        } catch (Exception e) {
            log.error("mysqlToRedisJobHandler执行异常: ", e);
            return ReturnT.ofFail(e.getMessage());
        }
    }

}
